//
//  LCHomeOriginalView.swift
//  SinaWeiBo
//
//  Created by mac on 16/12/5.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import SDWebImage

class LCHomeOriginalView: UIView {
    
    //记录约束的变量
    private var currentConstaint: Constraint?
    
    //定义接口接收cell传过来的数据
    var originalModel:LCHomeCellViewModel?{
        //接收到数据的时候给子控件赋值
        didSet{
            //头像
            if let urlStr = originalModel?.statusess?.user?.profile_image_url{
                userImageView.sd_setImage(with: URL(string: urlStr), placeholderImage: UIImage(named:"avatar_default_big"))
            }
            //昵称
            userName.text = originalModel?.statusess?.user?.name
            //内容
            contentLabel.text = originalModel?.statusess?.text
            //会员等级图
            mbrankImageView.image = originalModel?.mbrankImage
            //认证等级图
            verifiedImageView.image = originalModel?.verifiedTypeImage
            //来源
            sourceLabel.text = originalModel?.contentSource
            
            //先卸载已有的约束
            currentConstaint?.uninstall()
            if let prictures = originalModel?.statusess?.pic_urls,prictures.count > 0{
                //有图片
    
                prictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    currentConstaint = make.bottom.equalTo(prictureView).offset(StatusItemMargin).constraint
                })
             
                   prictureView.prictureList = originalModel?.statusess?.pic_urls
            }else{
                //没有图片
                
                prictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    currentConstaint = make.bottom.equalTo(contentLabel).offset(StatusItemMargin).constraint
                })
            }

            
        }
    }
    
    //懒加载控件
    //1用户头像
    private lazy var userImageView:UIImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
    //2用户名称
    private lazy var userName:UILabel = {
       let name = UILabel()
        name.text = "李超"
        name.textColor = UIColor.darkGray
        name.font = UIFont.systemFont(ofSize: 14)
        return name
    }()
    //3会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //4认证等级
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //5发布时间
    private lazy var timeLabel: UILabel = {
       let time = UILabel()
        time.text = "刚刚"
        time.textColor = UIColor.orange
        time.font = UIFont.systemFont(ofSize: 11)
        return time
    }()
    
    //6来源
    private lazy var sourceLabel: UILabel = {
        let source = UILabel()
        source.text = "来自:✨✨✨✨✨✨"
        source.textColor = UIColor.gray
        source.font = UIFont.systemFont(ofSize: 11)
        return source
    }()
    //7内容
    private lazy var contentLabel: UILabel = {
        let content = UILabel()
        content.text = "覅符合一护额覅恶化菲菲姐付家饿哦if见噢诶见佛IE风风绯闻绯闻绯闻绯闻"
        content.textColor = UIColor.darkGray
        content.font = UIFont.systemFont(ofSize: 13)
        content.numberOfLines = 0
        return content
    }()
    
    //8展示图片
    private lazy var prictureView:LCHomePictureView = {
       let pricture = LCHomePictureView()
        
        return pricture
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        //设置背景颜色
        backgroundColor = UIColor.white
        
        //添加子控件
        addSubview(userImageView)
        addSubview(userName)
        addSubview(mbrankImageView)
        addSubview(verifiedImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(prictureView)
        //添加约束
        userImageView.snp_makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(StatusItemMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        userName.snp_makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusItemMargin)
        }
        mbrankImageView .snp_makeConstraints { (make) in
            make.leading.equalTo(userName.snp_trailing).offset(StatusItemMargin)
            make.top.equalTo(userImageView)
        }
        verifiedImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusItemMargin)
            make.bottom.equalTo(userImageView)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusItemMargin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(StatusItemMargin)
//            make.bottom.equalTo(self)
            make.width.equalTo(ScreenWidth - 2*StatusItemMargin)
        }
        prictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusItemMargin)
            make.leading.equalTo(self)
        }
        self.snp_makeConstraints { (make) in
            currentConstaint = make.bottom.equalTo(prictureView).offset(StatusItemMargin).constraint
        }
        
    }
    
}
